package org.yinweichen.llm_dataset_backend.repository;

import org.yinweichen.llm_dataset_backend.entity.Version;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface VersionRepository extends JpaRepository<Version, String> {

    // 根据描述模糊查询
    List<Version> findByDescriptionContaining(String keyword);

    // 查询已发布或未发布的版本
    List<Version> findByPublished(Boolean published);

    // 按时间范围查询
    List<Version> findByTimeBetween(Date start, Date end);

    // 查询最新版本
    @Query("SELECT v FROM Version v ORDER BY v.time DESC LIMIT 1")
    Version findLatestVersion();

    // 更新发布状态
    @Modifying
    @Query("UPDATE Version v SET v.published = true , v.time = :time WHERE v.id = :id AND v.published = false")
    int publishVersion(String id, Date time);

    //查询已/未发布的版本
    @Query("SELECT v FROM Version v WHERE v.published = :published ORDER BY v.time DESC LIMIT 1")
    List<Version> findByPublished(boolean published);

    @Query("SELECT v FROM Version v ORDER BY v.time DESC")
    List<Version> findAllVersions();
}
